package cc.linker.steplink.crm.merchant.service;

import cc.linker.steplink.crm.merchant.dao.SurvivalPcDao;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SurvivalPcService {

    @Autowired
    private SurvivalPcDao survivalDao;


    /**
     * 根据菜单id删除促活（默认菜单）
     * @param orgId
     */
    public void deleteByDefaultMenuId(String orgId) {
        Map  menuDefaultMap= survivalDao.getDefaultMuneDataListByOrgId(orgId);
        String menuDataString ="";
        if(menuDefaultMap!=null && !StringUtils.isEmpty(menuDefaultMap.get("menuDataString"))){
            menuDataString = menuDefaultMap.get("menuDataString").toString();
        }
        Long menuId =0L;
        List<String> eventKeyList = new ArrayList();
        if(!StringUtils.isEmpty(menuDataString)){
            Map object = JSON.parseObject(menuDataString,Map.class);
            if(!StringUtils.isEmpty(object.get("button"))){
                List<Map> menuDataListO = JSONArray.parseArray(object.get("button").toString(),Map.class);
                for (Map menuMap : menuDataListO){
                    if(menuMap.containsKey("sub_button") && !StringUtils.isEmpty(menuMap.get("sub_button"))){
                        //有二级菜单
                        String subButton = menuMap.get("sub_button").toString();
                        Map subButtonObj = JSON.parseObject(subButton,Map.class);
                        if(subButtonObj.containsKey("list") && !StringUtils.isEmpty(subButtonObj.get("list"))){
                            String listString = subButtonObj.get("list").toString();
                            List<Map> subButtonListO = JSONArray.parseArray(listString,Map.class);
                            for(Map subButtonMap : subButtonListO){
                                if(!StringUtils.isEmpty(subButtonMap.get("key"))){
                                    //只需要clickPushEvent类型的
                                    if(subButtonMap.get("key").toString().contains("clickPushEvent")){
                                        eventKeyList.add(subButtonMap.get("key").toString());
                                    }
                                }
                            }
                        }
                    }else {
                        //无二级菜单
                        if(!StringUtils.isEmpty(menuMap.get("key"))){
                            //只需要clickPushEvent类型的
                            if(menuMap.get("key").toString().contains("clickPushEvent")){
                                eventKeyList.add(menuMap.get("key").toString());
                            }
                        }
                    }
                }
            }
        }
        String eventKeys="";
        if(eventKeyList!=null && eventKeyList.size()>0){
            eventKeys = String.join(",", eventKeyList);
        }
        Map map = new HashMap();
        map.put("menuId",menuId);
        map.put("eventKeys",eventKeys);
        map.put("orgId",orgId);
        //删除需要删除的促活
        survivalDao.deleteByMenuId(map);
}



    /**
     * 根据菜单id删除促活（个性化）
     * @param menuId
     */
    public void deleteByMenuId(Long menuId) {
        String menuDataString = survivalDao.getMuneDataList(menuId);
        List<String> eventKeyList = new ArrayList();
        if(!StringUtils.isEmpty(menuDataString)){
            Map object = JSON.parseObject(menuDataString,Map.class);
            if(!StringUtils.isEmpty(object.get("button"))){
                List<Map> menuDataListO = JSONArray.parseArray(object.get("button").toString(),Map.class);
                for (Map menuMap : menuDataListO){
                    if(menuMap.containsKey("sub_button") && !StringUtils.isEmpty(menuMap.get("sub_button"))){
                        //有二级菜单
                        String subButton = menuMap.get("sub_button").toString();
                        List<Map> subButtonListO = JSONArray.parseArray(subButton,Map.class);
                        for(Map subButtonMap : subButtonListO){
                            if(!StringUtils.isEmpty(subButtonMap.get("key"))){
                                //只需要clickPushEvent类型的
                                if(subButtonMap.get("key").toString().contains("clickPushEvent")){
                                    eventKeyList.add(subButtonMap.get("key").toString());
                                }
                            }
                        }
                    }else {
                        //无二级菜单
                        if(!StringUtils.isEmpty(menuMap.get("key"))){
                            //只需要clickPushEvent类型的
                            if(menuMap.get("key").toString().contains("clickPushEvent")){
                                eventKeyList.add(menuMap.get("key").toString());
                            }
                        }
                    }
                }
            }
        }
        String eventKeys="";
        if(eventKeyList!=null && eventKeyList.size()>0){
            eventKeys = String.join(",", eventKeyList);
        }
        Map map = new HashMap();
        map.put("menuId",menuId);
        map.put("eventKeys",eventKeys);
        //删除需要删除的促活
        survivalDao.deleteByMenuId(map);
    }

}
